package com.systop.tea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systop.tea.common.R;
import com.systop.tea.dto.CategoryDto;
import com.systop.tea.entity.Category;
import com.systop.tea.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/category")
public class CategoryController {

    private final CategoryService categoryService;

    @GetMapping("/query")
    public R<Page> query(Category category, Integer pageNum, Integer pageSize) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 50;
        Page<Category> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if (category.getCategoryTopId() != null) {
            if (category.getCategoryTopId() != -1)
                queryWrapper.eq(Category::getCategoryTopId, category.getCategoryTopId());
            else
                queryWrapper.ne(Category::getCategoryTopId, 0);
        }
        categoryService.page(pageInfo, queryWrapper);

        Page<CategoryDto> categoryDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, categoryDtoPage, "records");

        List<Category> records = pageInfo.getRecords();
        List<CategoryDto> list = records.stream().map((item) -> {
            CategoryDto categoryDto = new CategoryDto();
            BeanUtils.copyProperties(item, categoryDto);
            Category topCategory = categoryService.getById(item.getCategoryTopId());
            if (topCategory != null)
                categoryDto.setCategoryTopName(topCategory.getCategoryName());
            else
                categoryDto.setCategoryTopName("无");
            return categoryDto;
        }).collect(Collectors.toList());

        categoryDtoPage.setRecords(list);
        return R.success(categoryDtoPage);
    }

    @GetMapping("/{id}")
    public R<Category> queryById(@PathVariable Integer id) {
        Category category = categoryService.getById(id);
        return R.success(category);
    }

    @PostMapping
    public R<String> add(@RequestBody Category category) {
        boolean row = categoryService.save(category);
        if (row) {
            return R.success("分类添加成功");
        } else {
            return R.error("分类添加失败");
        }
    }

    @PutMapping
    public R<String> modify(@RequestBody Category category) {
        boolean row = categoryService.updateById(category);
        if (row) {
            return R.success("分类修改成功");
        } else {
            return R.error("分类修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Integer id) {
        boolean rows = categoryService.removeById(id);
        if (rows) {
            return R.success("分类删除成功");
        } else {
            return R.error("分类删除失败");
        }
    }

}
